<extend name="../layout" />

<block name="main_content">
    <form id="add_order_form" class="form-horizontal" method="POST" action="__URL__/insert">
        <div class="row-fluid">
            <div class="span8">
                <div class="widget-box">
                    <div class="widget-title"> <span class="icon"> <i class="icon-info-sign"></i> </span>
                        <h5>{:L('add_new_returns')}</h5>
                    </div>
                    <div class="widget-content nopadding">
                        <div class="control-group">
                            <label class="control-label">{:L('subject')}:</label>
                            <div class="controls">
                                <input type="text" class="span11" name="subject" required />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">{:L('customer')}:</label>
                            <div class="controls">
                                <input type="text" class="span5" value="{$Think.get.customer_name}" id="auto_search_customer" required />
                                <a href="#selectCustomerModal" class='btn btn-primary'>{:L('select')}</a>
                                <a href="#addCustomerModel" data-toggle="modal" class="btn btn-success">{:L('add_new')}</a>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">{:L('stock')}:</label>
                            <div class="controls">
                                <html:select options="theStocks" name="stock_id" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">{:L('returns_type')}:</label>
                            <div class="controls">
                                <html:select options="ReturnsType" name="returns_type" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">{:L('memo')}:</label>
                            <div class="controls">
                                <textarea class="span11" name="memo"></textarea>
                            </div>
                        </div>
                        <div class="form-actions">
                            <input type="hidden" name="customer_id" id="customer_id" value="{$Think.get.customer_id}" />
                            <button type="submit" class="btn btn-success">{:L('Save')}</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="span3">
            </div>
            <div class="span4">
            </div>
        </div>
    </form>


    <div id="addCustomerModel" class="modal hide">
        <div class="modal-header">
            <button data-dismiss="modal" class="close" type="button">×</button>
            <h3>{:L('add_new_customer')}</h3>
        </div>
        <form action="{:U('/CRM/Customer/insert')}" method="POST">
            <div class="modal-body">
                <div style="width:100px;">
                <include file="CRM:Customer:addCustomerFormElements" />
                </div>
            </div>
            <div class="modal-footer"> 
                <input type="hidden" name="goto" value="/JXC/Orders/add" />
                <button type="submit" class="btn btn-primary">{:L('save')}</button> 
                <a data-dismiss="modal" class="btn" href="#">{:L('cancle')}</a> 
            </div>
        </form>
    </div>

</block>

<block name="footer_js">
    <script type="text/javascript">
        function assign_customerLinkman(id) {
            $.get("{:U('/CRM/CustomerLinkman/ajax_getLinkman')}", {
                customer_id : id
            }, function(data) {
                var _html = $("<select />");
                var _option;
                _html.attr("name","customer_linkman_id");
                for(i=0;i<data.length;i++) {
                    _option = $("<option />");
                    _option.attr("value", data[i].id)
                    _option.text(data[i].contact);
                    _html.append(_option);
                }
                $("#auto_search_customer").parent().find("select").remove();
                $("#auto_search_customer").after(_html);
            });
        }
        $(function() {
            $("#add_order_form").submit(function(){
                if(!$("#customer_id").val()) {
                    alert("请选择客户");
                    return false;
                }
            });
            $("#auto_search_customer").autocomplete({
                source: "{:U('/CRM/Customer/ajax_getCustomers')}",
                minLength: 1,
                select: function(event, ui) {
                    $("#customer_id").val(ui.item.id);
                    assign_customerLinkman(ui.item.id);
                }
            });
            <if condition="$_GET[customer_id]">
                assign_customerLinkman('{$Think.get.customer_id}');
            </if>
        });
    </script>
</block>